package com.jiyun.controller;

import com.jiyun.auth.entity.UserInfo;
import com.jiyun.auth.utils.JwtUtils;
import com.jiyun.config.JwtProperties;
import com.jiyun.pojo.entity.User;
import com.jiyun.pojo.vo.UserVo;
import com.jiyun.service.AuthService;
import com.jiyun.utils.CookieUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@EnableConfigurationProperties(JwtProperties.class)
public class AuthController {

    @Autowired
    private AuthService authService;

    @Autowired(required = false)
    private JwtProperties prop;

    //手机号登录
    @RequestMapping("accreditphone")
    public ResponseEntity<Void> shoujidenglu(
            @RequestBody UserVo userVo,
            HttpServletRequest request,
            HttpServletResponse response) {
        // 登录校验
        System.out.println(userVo.getPhone());
        System.out.println(userVo);
        String token = this.authService.shoujidenglu(userVo);

        if (StringUtils.isBlank(token)) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
        // 将token写入cookie,并指定httpOnly为true，防止通过JS获取和修改
        CookieUtils.setCookie(request, response, prop.getCookieName(),
                token, prop.getCookieMaxAge(), null, true);
        return ResponseEntity.ok().build();
    }

    //账号密码登录
    @RequestMapping("accredit")
    public ResponseEntity<Void> authentication(
            @RequestBody User user,
            HttpServletRequest request,
            HttpServletResponse response) {
        // 登录校验
        String token = this.authService.authentication(user);
        if (StringUtils.isBlank(token)) {
            return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
        }
        // 将token写入cookie,并指定httpOnly为true，防止通过JS获取和修改
        CookieUtils.setCookie(request, response, prop.getCookieName(),
                token, prop.getCookieMaxAge(), null, true);
        System.out.println("token_______________"+token);
        return ResponseEntity.ok().build();
    }

    /**
     * 用户操作，需要刷新token
     * @param token
     * @param request
     * @param response
     * @return
     */
    @GetMapping("verify")
    public ResponseEntity<UserInfo> getUserInfo(
            @CookieValue("LY_TOKEN") String token,
            HttpServletRequest request,
            HttpServletResponse response
    ){
        try {
            UserInfo userInfo = JwtUtils.getInfoFromToken(token, prop.getPublicKey());
            //只要用户有这个请求，就给用户一个新的token，防止过期
            String newToken = JwtUtils.generateToken(userInfo, prop.getPrivateKey(), prop.getExpire());
            //这里的true谁不允许js操作的意思
            CookieUtils.setCookie(request,response,prop.getCookieName(),
                    newToken,prop.getCookieMaxAge(),null,true);
            return ResponseEntity.status(HttpStatus.OK).body(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
        }
    }
}
